package com.adkfp.action;

import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.adkfp.utils.JsonUtil;
import com.adkfp.utils.LayuiUtil;
import com.adkfp.bean.ADKCardInfoBean;
import com.adkfp.bean.ADKUserInfoBean;
import com.adkfp.service.IADKCardInfoService;
import com.adkfp.service.IADKUserInfoService;

/**
 * class name:ADKCardInfoAction <BR>
 * class description: ADKCardInfo表的增删改查 <BR>
 * Remark: <BR>
 * @version 1.00 2019年01月03日
 * @author JACK)jackwei
 */
@Controller
@RequestMapping("aDKCardInfo")
public class ADKCardInfoAction {
	@Resource(name="aDKCardInfoService")
	private IADKCardInfoService aDKCardInfoService;

	@Resource(name="aDKUserInfoService")
	private IADKUserInfoService aDKUserInfoService;
  /**
 	* Method name: selectAll <BR>
 	* Description: 查询所有记录 <BR>
 	* Remark: <BR>
 	* @param bean
 	* @return  Object<BR>
 	*/
	@RequestMapping("selectAll")
	@ResponseBody
	public Object selectAll(ADKCardInfoBean bean,HttpServletRequest request,String userid) {
		//System.out.println("-----------"+userid);
		bean.setUserID(userid);
		List<ADKCardInfoBean> list = new ArrayList<>();
		list = aDKCardInfoService.findByCondition(bean);
		long count = aDKCardInfoService.findByConditionCount(bean);
		//System.err.println(count);
		return LayuiUtil.data(count, list);
	}
	
	/**
	 * Method name: index <BR>
	 * Description: 转发到页面 <BR>
	 * Remark: <BR>
	 * @return  String<BR>
	 */
	
	@RequestMapping("index")
	public String index() {
		return "aDKCardInfo/aDKCardtree";
	}
	

	
	@RequestMapping("findId2")
	public Object findId2(HttpServletRequest request,String depid,ADKUserInfoBean bean) {
		System.out.println("呵呵呵呵呵----------------"+depid);
		bean.setDepid(depid);
		request.setAttribute("u",bean);
		return "aDKCardInfo/selectCard";
	}
	
	/**
	 * Method name: options <BR>
	 * Description: ADKCardInfo表的增删改 <BR>
	 * Remark: <BR>
	 * @param request
	 * @param response
	 * @return  String<BR>
	 */
	@RequestMapping("options")
	@ResponseBody
	public String options(@RequestParam("op")String op, @RequestParam("data")String data) {
		String f = "";
		
		switch (op) {
			case "add":
				if(data!=null&&!data.equals("")) {
					List<ADKCardInfoBean> list = JsonUtil.getList4Json(data, ADKCardInfoBean.class);
					for (ADKCardInfoBean bean : list) {
						aDKCardInfoService.insert(bean);
					}
					f = "增加成功";
				}
				break;
			case "del":
				if(data!=null&&!data.equals("")) {
					List<ADKCardInfoBean> list = JsonUtil.getList4Json(data, ADKCardInfoBean.class);
					for (ADKCardInfoBean bean : list) {
						aDKCardInfoService.delete(bean.getCardID());
					}
					f = "删除成功";
				}
				break;
			case "upd":
				if(data!=null&&!data.equals("")) {
					List<ADKCardInfoBean> list = JsonUtil.getList4Json(data, ADKCardInfoBean.class);
					for (ADKCardInfoBean bean : list) {
						aDKCardInfoService.update(bean);
					}
					f = "更新成功";
				}
				break;
			default:
				break;
		}
		return f;
	}
	
	
	//用于跳转卡片管理页面(查找userid)
	@RequestMapping("findId")
	public String findId(HttpServletRequest request,String userid,ADKUserInfoBean bean) {
		if(bean.getAutoid() != null && !bean.getAutoid().equals("")) {
			bean=aDKUserInfoService.finduseridById(userid);
		}
		
		
		request.setAttribute("userid",bean);
		return "aDKCardInfo/Card";
	}
	
	//跳转页面
	@RequestMapping("addPage")
	public String addPage(String userid,HttpServletRequest request,ADKCardInfoBean bean) {	
		System.out.println("+++++++++"+userid);
		bean.setUserID(userid);
		request.setAttribute("u",bean);
		return "aDKCardInfo/addCard";
	}
	
	//卡片的增加
	@RequestMapping("addCardInfo")
	@ResponseBody
	public void addCardInfo(ADKCardInfoBean bean,String userid) {	
		System.err.println("----------"+userid);
		    bean.setUserID(userid);
			aDKCardInfoService.insert(bean);
			
}
	//卡片的修改
	@RequestMapping("updateCardInfo")
	@ResponseBody
	public void updateCardInfo(Integer cardID1,Integer cardID) {
		aDKCardInfoService.updateCard(cardID1,cardID);
	}


	
	//换卡页面
	@RequestMapping("huanCardInfo")
	public String huanCardInfo(HttpServletRequest request,String cardID,ADKCardInfoBean cardbean) {
		cardbean.setCardID(Integer.parseInt(cardID));
		request.setAttribute("huan",cardbean );
		return "aDKCardInfo/huanCard";
}
	//修改卡状态
	@RequestMapping("updateflag")
	@ResponseBody
	public void updateflag(@RequestParam("cardID")Integer cardID,Integer flag) {
		//System.out.println("-------"+cardID);
		aDKCardInfoService.updateflag(cardID,flag);
		
	}
	
}